package com.framework.shs.commom.base.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.HibernateCallback;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.framework.shs.commom.base.BaseDao;
import com.framework.shs.commom.paginate.PageData;
import com.framework.shs.commom.util.GenericsUtils;

@SuppressWarnings("all")
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

	protected Log logger = LogFactory.getLog(BaseDaoImpl.class);

	protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this
			.getClass());

	@Resource(name="sessionFactory")
	public final void setSessionFactoryMe(SessionFactory sessionFactory){
		sessionFactory.openSession().setFlushMode(FlushMode.AUTO);
		super.setSessionFactory(sessionFactory);
	}
	public void clear() {
		getHibernateTemplate().clear();
	}

	public void fulsh() {
		getHibernateTemplate().flush();
	}

	public Session getSession(){
		return getHibernateTemplate().getSessionFactory().getCurrentSession();
	}
	
	public void delete(Serializable... entityids) {
		for (int i = 0; i < entityids.length; i++) {
			T t = get(entityids[i]);
			if (t != null) {
				getHibernateTemplate().delete(t);
			}
		}
	}

	public T find(Serializable entityId) {
		if (entityId == null) {
			throw new RuntimeException(this.entityClass.getName()
					+ ":id is empty or null");
		}
		T t = null;
		t = get(entityId);
		return t;
	}

	public T get(Serializable entityId) {
		if (entityId == null) {
			throw new RuntimeException(this.entityClass.getName()
					+ ":id is empty or null");
		}
		return (T) getHibernateTemplate().get(this.entityClass, entityId);
	}


	/**
	 * 设置查询条件的参数
	 * 
	 * @param query
	 * @param queryParams
	 */
	protected static void setQueryParams(Query query, Object[] queryParams) {
		if (queryParams != null && queryParams.length > 0) {
			for (int i = 0; i < queryParams.length; i++) {
				query.setParameter(i, queryParams[i]);
			}
		}
	}

	/**
	 * 获取实体的名称
	 * 
	 * @param <E>
	 * @param clazz
	 *            实体类
	 * @return
	 */
	protected static <E> String getEntityName(Class<E> clazz) {
		String entityname = clazz.getSimpleName();
		return entityname;
	}

	public List<T> findAll() {
		List<T> list = getHibernateTemplate().loadAll(entityClass);
		return list;
	}

	public List<T> findTbyHql(String hql) {
		List<T> list = (List<T>) getHibernateTemplate().find(hql);
		return list;
	}

	public int executeDML(final String hql) {
		Integer result = 0;
		result = getHibernateTemplate().bulkUpdate(hql);
		return result;
	}

	public int executeDML(final String sethql, Object[] values) {
		Integer result = 0;
		final String entityname = getEntityName(this.entityClass);
		String hql = "update " + entityname + " o " + sethql;
		result = getHibernateTemplate().bulkUpdate(hql, values);
		return result;
	}

	public <T> void delete(T t) {
		getHibernateTemplate().delete(t);
	}

	public <T> T get(Class<T> clazz, String id) {
		return (T) getHibernateTemplate().get(clazz,id);
	}

	public <T> void update(T t) {
		getHibernateTemplate().update(t);
	}

	public <T> T save(T t) {
		 getHibernateTemplate().saveOrUpdate(t);
		 clear();
		 return t;
	}
}
